<template>
  <div class="container">
    <div class="app-container">
      <el-tree :expand-on-click-node="false" :data="list" :props="defaultProps" default-expand-all>
        <template #default="{ data }">
          <el-row type="flex" justify="space-between" align="middle" style="width:100%; height:40px">
            <span>{{ data.name }}</span>
            <el-col :span="4" class="col-manager">
              <span class="tree-manager">{{ data.managerName }}</span>
              <el-dropdown @command="handleCommand">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item :command="{ type: 'add', id: data.id }">添加子部门</el-dropdown-item>
                  <el-dropdown-item :command="{ type: 'edit', id: data.id }">编辑部门</el-dropdown-item>
                  <el-dropdown-item :command="{ type: 'del', id: data.id }">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>

    <!-- 弹出层 -- 部门 -->
    <FormDialog :id="id" ref="formDialog" :dialog-form-visible.sync="dialogFormVisible" @onSubmit="getDepartmentsFn" />
  </div>
</template>
<script>
import { getDepartments, delDepartments } from '@/api/department'
import { getTree } from '@/utils/index'
import FormDialog from './components/FormDialog.vue'
export default {
  name: 'Department',
  components: {
    FormDialog
  },
  data() {
    return {
      // 部门列表
      list: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      id: null,
      dialogFormVisible: false
    }
  },
  created() {
    this.getDepartmentsFn()
  },
  methods: {
    // 获取部门列表
    async getDepartmentsFn() {
      const res = await getDepartments()
      this.list = getTree(res, 0)
    },
    // 打开弹出
    handleCommand(command) {
      // console.log(command)
      // console.log(this.id)
      if (command.type === 'add') {
        this.id = command.id
        this.dialogFormVisible = true
      } else if (command.type === 'edit') {
        this.id = command.id
        this.dialogFormVisible = true
        this.$nextTick(() => {
          this.$refs.formDialog.getDetialFn()
        })
      }
      // 删除部门
      if (command.type === 'del') {
        this.$confirm('此操作将永久删除该部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteDepartments(command.id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
      }
    },
    // 删除部门
    async deleteDepartments(id) {
      console.log(id)
      await delDepartments(id)
      // 重新获取数据 渲染
      this.getDepartmentsFn()
    }
  }
}
</script>

<style scoped lang="scss">
.app-container {
  padding: 30px 140px;
  font-size: 14px;

  .col-manager {
    display: flex;
    justify-content: space-around;
    align-items: center;
    width: 200px;

    .tree-manager {
      width: 50px;
      display: inline-block;
      margin: 10px;
    }
  }
}

</style>
